package com.anan.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// 拦截器
public class Interceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();  // 会话
        // 获取会话信息 校验登录


//        StringBuffer requestURL = request.getRequestURL();  // 请求完整url
//        String contextPath = request.getContextPath();  // 上下文路径 /api
//        String servletPath = request.getServletPath();  // 请求路径 例如 /login
//        System.out.println(requestURL);
//        System.out.println(contextPath);
//        System.out.println(servletPath);


        return true;
//        if (requestURL != null) {
//            return true;  // 放行
//        } else {
//            // 重定向
////            request.getRequestDispatcher("/").forward(request,response);
//            return false;  // 拦截
//        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
